//
//  RootViewController.m
//  Demo
//
//  Created by 王跃 on 2017/12/27.
//  Copyright © 2017年 WY. All rights reserved.
//

#import "RootViewController.h"
#import "Customer_UITabBar.h"
/*视图宽高*/
#define WY_SCREENWIDTH  [UIScreen mainScreen].bounds.size.width
#define WY_SCREENHEIGHT  [UIScreen mainScreen].bounds.size.height

@interface RootViewController ()<CustomerTabBarDelegate>
@property (nonatomic, strong) NSMutableArray *childVCTitleArray;

@property (nonatomic, strong) NSMutableArray *childVCArray;
@property (nonatomic, strong) Customer_UITabBar *wyTabbar;
@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    //添加子控制器
    [self addChild];
    [self setDefaltData];
}

- (void)setDefaltData
{
    //去掉白线
    [[UITabBar appearance] setShadowImage:[UIImage new]];
    [[UITabBar appearance] setBackgroundImage:[UIImage new]];

    //将自定义tabba加载到当前的tabba控制器上
    [self.tabBar addSubview:self.wyTabbar];
    self.wyTabbar.titleArray = self.childVCTitleArray;
    self.selectedIndex = 1;
}

#pragma mark  初始化自定义的tabbar

- (Customer_UITabBar *)wyTabbar
{
    if (!_wyTabbar)
    {
        _wyTabbar = [[Customer_UITabBar alloc]init];
        _wyTabbar.backgroundColor = [UIColor whiteColor];
        _wyTabbar.frame = CGRectMake(0,
                                     0,
                                     self.tabBar.width,
                                     TAB_BAR_HEIGHT);
        _wyTabbar.delegate = self;
    }
    return _wyTabbar;
}

- (NSMutableArray *)childVCTitleArray
{
    if (!_childVCTitleArray)
    {
        _childVCTitleArray = [NSMutableArray arrayWithObjects:
                              @"排行",
                              @"直播",
                              @"财富",
                              @"视频聊",
                              @"我的",
                              nil];
    }
    return _childVCTitleArray;
}

- (void)addChild
{
    /*如果是Storyboard 需要将Storyboard的名字和ID设置一样  调用setTabbarControllerWithStoryboardName

     如果是手写代码将控制器名字传入  调用setTabbarControllerWithName：@"控制器名字"
     */

    NSArray *ncArray = @[[self setTabbarControllerWithName:@"RankViewController"],
                         [self setTabbarControllerWithName:@"LiveViewController"],
                         [self setTabbarControllerWithName:@"RichViewController"],
                         [self setTabbarControllerWithName:@"VideoViewController"],
                         [self setTabbarControllerWithName:@"OwnerViewController"]];
    self.viewControllers = ncArray;
}

#pragma mark  自定义方法   不需要导入控制，只需要将控制器的名字传入即可

- (UINavigationController *)setTabbarControllerWithName:(NSString *)name
{
    return [[UINavigationController alloc]initWithRootViewController:[[NSClassFromString(name) alloc] init]];
}

- (UINavigationController *)setTabbarControllerWithStoryboardName:(NSString *)name
{
    return [[UINavigationController alloc]initWithRootViewController:[[UIStoryboard storyboardWithName:name bundle:nil] instantiateViewControllerWithIdentifier:name]];
}


#pragma mark - CustomerTabBarDelegate

- (void)tabbar:(Customer_UITabBar *)tabbar clickButton:(NSInteger)index
{
    self.selectedIndex = index;
}

@end

